# for TMail 1.1.1 (actionmailer 2.0.2)
# あいまいMADDRESSパーサ
# http://d.hatena.ne.jp/urekat/20071030/1193728474#tb
class << TMail::Parser
  alias :parse_without_ambiguous :parse
  def parse_with_ambiguous( ident, str, cmt = nil )
    unless [:ADDRESS, :MADDRESS].include?(ident)
      parse_without_ambiguous(ident, str, cmt)
    else
      mobile_domains = '(?:docomo\.ne\.jp|ezweb\.ne\.jp|softbank\.ne\.jp|[dhtcrknsq]\.vodafone\.ne\.jp)'
      #addr_regrxp = /([^<>"]+)@(#{mobile_domains}|[^<>"\s]+)/
      mobile_domain_regrxp = /#{mobile_domains}/
      addresses = []
      str.split(/,/).each do |part|
        name = local = domain = nil
        case part
        when /^(.+)\s?<([^<>"]+)@([^<>"@]+)>$/
          name = $1; local = $2; domain = $3
        when /([^<>"@]+)@([^<>"@]+)/
          local = $1; domain = $2
        else
          raise TMail::SyntaxError.new("parse error.")
        end
        addresses << if domain.match(mobile_domain_regrxp)
          address = TMail::Address.new(local.split(/\./), domain)
          def address.spec        ; @__spec      end
          def address.address     ; @__spec      end
          def address.__spec=(_s) ; @__spec = _s end
          address.__spec = if name
                             "#{name} <#{local}@#{domain}>"
                           else
                             "#{local}@#{domain}"
                           end
          address
        else
          parse_without_ambiguous(ident, part, cmt).first
        end
      end
      addresses
    end
  end
  alias :parse :parse_with_ambiguous
end
